/* 代理模式 */

pub trait Car {
    fn drive(&self, distance: u32);
}

pub struct Benz {}

impl Car for Benz {
    fn drive(&self, distance: u32) {
        println!("行驶了 {} 公里", distance);
    }
}

pub struct CarProxy {
    car: Box<dyn Car>
}

impl Car for CarProxy {
    fn drive(&self, distance: u32) {
        println!("启动");
        self.car.drive(distance);
        println!("停止");
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_proxy_1() {
        let car = Benz{};
        car.drive(10);
    }

    #[test]
    fn test_proxy_2() {
        let target = Benz{};
        let car = CarProxy {
            car: Box::new(target)
        };
        car.drive(20);
    }
}